package com.leetcode.partition5;

/**
 * @author `RKC`
 * @date 2021/8/3 9:38
 */
public class LC485最大连续1的个数 {

    public static int findMaxConsecutiveOnes(int[] nums) {
        return dynamicProgramming(nums);
    }

    //动态规划
    private static int dynamicProgramming(int[] nums) {
        int[] dp = new int[nums.length];
        if (nums[0] == 0) dp[0] = 0;
        else dp[0] = 1;
        int max = dp[0];

        for (int i = 1; i < nums.length; i++) {
            if (nums[i] == 0) dp[i] = 0;
            else dp[i] = dp[i - 1] + 1;
            max = Math.max(max, dp[i]);
        }
        return max;
    }

    //迭代法
    private static int method1(int[] nums) {
        int count = 0, result = 0;
        for (int num : nums) {
            if (num == 0) {
                result = Math.max(count, result);
                count = 0;
            } else {
                count++;
            }
        }
        result = Math.max(count, result);
        return result;
    }

    public static void main(String[] args) {
        int[] nums = {1, 1, 0, 1, 1, 1};
        System.out.println(findMaxConsecutiveOnes(nums));
    }
}
